package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.manage.model.Ttopic;
import com.xy.manage.service.TreplyService;
import com.xy.manage.service.TtopicService;



@Controller
public class TtopicAction{
	private static final Logger log = LoggerFactory.getLogger(TtopicAction.class);
	public TtopicAction() {
	}
 
 
 	/**
	 * 民意警务论坛话题表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttopicAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		Ttopic t =  new Ttopic();
		t.setSeq(0);
		model.addAttribute("ttopic", t);
        setDict(model);
		return "ttopicEdit";
	}

	/**
	 * 民意警务论坛话题表新增保存
	 * 
	 * @param ttopic
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_ttopicSave.do")
	public String save(Ttopic ttopic, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(ttopic, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ttopic = ttopicService.save(ttopic);
		return "redirect:v_ttopicList.do";
	}

	/**
	 * 民意警务论坛话题表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_ttopicDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				ttopicService.remove(id);
			}
		}
		return "redirect:v_ttopicList.do";
	}

	/**
	 * 民意警务论坛话题表编辑页面
	 * 
	 * @param tid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttopicEdit.do")
	public String edit(long tid, HttpServletRequest request, ModelMap model) {
		Ttopic ttopic = ttopicService.get(tid);
		model.addAttribute("ttopic", ttopic);
		model.addAttribute("actionEdit", request.getParameter("actionEdit"));
		
		setDict(model);
		return "ttopicEdit";
	}

	/**
	 * 民意警务论坛话题表详情
	 * 
	 * @param tid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttopicDetail.do")
	public String detail(long tid,Integer pageNo, HttpServletRequest request, ModelMap model) {
		Ttopic ttopic = ttopicService.get(tid);
		model.addAttribute("ttopic", ttopic);
		// 查询相关评论
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ttopic", ttopic);
				if (pageNo == null)
					pageNo = 1;
				Pagination pagination = treplyService.getTreplyByPage(map, pageNo, CookieUtils.getPageSize(request));
				model.addAttribute("pagination", pagination);
        setDict(model);
        
		return "ttopicView";
	}

	/**
	 * 民意警务论坛话题表查询（分页）
	 * 
	 * @param ttopic
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttopicList.do")
	public String listPage(Ttopic ttopic, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = ttopicService.getTtopicByPage(toMap(ttopic),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "ttopicList";
		
		
	}

	/**
	 * 民意警务论坛话题表查询
	 * 
	 * @param ttopic
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttopicList1.do")
	public String list(Ttopic ttopic, HttpServletRequest request, ModelMap model) {
		List<Ttopic> list = ttopicService.getTtopic(toMap(ttopic));
		model.addAttribute("list", list);
        setDict(model);
		return "ttopicList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param ttopic
	 * @return
	 */	private Map<String,Object> toMap(Ttopic ttopic){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(ttopic.getTid()!=null&&!ttopic.getTid().equals("")){
			map.put("tid", ttopic.getTid());
		}
		if(ttopic.getTitle()!=null&&!ttopic.getTitle().equals("")){
			map.put("title", ttopic.getTitle());
		}
		if(ttopic.getContent()!=null&&!ttopic.getContent().equals("")){
			map.put("content", ttopic.getContent());
		}
		if(ttopic.getCreatetime()!=null&&!ttopic.getCreatetime().equals("")){
			map.put("createtime", ttopic.getCreatetime());
		}
		if(ttopic.getStatus()!=null&&!ttopic.getStatus().equals("")){
			map.put("status", ttopic.getStatus());
		}
		if(ttopic.getType()!=null&&!ttopic.getType().equals("")){
			map.put("type", ttopic.getType());
		}   
		if(ttopic.getUsername()!=null&&!ttopic.getUsername().equals("")){
			map.put("username", ttopic.getUsername());
		}  
		if(ttopic.getSeq()!=null&&!ttopic.getSeq().equals("")){
			map.put("seq", ttopic.getSeq());
		}  
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TTOPIC_STATUS));
      model.addAttribute("typeMap", DictUtils.getDictByType(DictUtils.TTOPIC_TYPE));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Ttopic ttopic, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   

	@Resource
	private TtopicService ttopicService;   
	@Resource
	private TreplyService treplyService;   
}